﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Testcraft
{
    class Block
    {
        
        public const ushort UninitializedBlockID = 0xFFFF;
        public const ushort AirID = 0xFFFE;

        public static Block UninitializedBlock = new Block(UninitializedBlockID);

        //public enum FaceMask : byte
        //{
        //    PositiveX = 1,
        //    PositiveY = 2,
        //    PositiveZ = 4,
        //    NegativeX = 8,
        //    NegativeY = 16,
        //    NegativeZ = 32
        //};

        public byte visibleFaces;
        public Block(ushort id)
        {
            this.Id = id;
            this.visibleFaces = 0;
        }

        //public bool IsFacevisible(Direction dir)
        //{
        //    FaceMask mask = 0;
        //    switch (dir)
        //    {
        //        case Direction.PositiveX:
        //            mask = FaceMask.PositiveX;
        //            break;
        //        case Direction.PositiveY:
        //            mask = FaceMask.PositiveY;
        //            break;
        //        case Direction.PositiveZ:
        //            mask = FaceMask.PositiveZ;
        //            break;
        //        case Direction.NegativeX:
        //            mask = FaceMask.NegativeX;
        //            break;
        //        case Direction.NegativeY:
        //            mask = FaceMask.NegativeY;
        //            break;
        //        case Direction.NegativeZ:
        //            mask = FaceMask.NegativeZ;
        //            break;
        //    }

        //    return (this.visibleFaces & (int)mask) > 0;
        //}

        public bool IsInvisible()
        {
            return this.visibleFaces == 0;
        }

        //public void SetFaceVisibility(Direction dir, bool visible)
        //{
        //    FaceMask mask = 0;
        //    switch (dir)
        //    {
        //        case Direction.PositiveX:
        //            mask = FaceMask.PositiveX;
        //            break;
        //        case Direction.PositiveY:
        //            mask = FaceMask.PositiveY;
        //            break;
        //        case Direction.PositiveZ:
        //            mask = FaceMask.PositiveZ;
        //            break;
        //        case Direction.NegativeX:
        //            mask = FaceMask.NegativeX;
        //            break;
        //        case Direction.NegativeY:
        //            mask = FaceMask.NegativeY;
        //            break;
        //        case Direction.NegativeZ:
        //            mask = FaceMask.NegativeZ;
        //            break;
        //    }

        //    if (visible)
        //        this.visibleFaces |= (byte)mask;
        //    else
        //        this.visibleFaces &= (byte)~mask;
        //}

        public static VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[]
        {
                // Front face
                new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.25f, 0.75f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.5f, 0.75f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.25f, 1.0f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0, 0, 1), new Vector2(0.5f, 1.0f)),
                // Top face
                new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector3(0, 1, 0), new Vector2(0.25f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector3(0, 1, 0), new Vector2(0.5f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(0, 1, 0), new Vector2(0.25f, 0.75f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0, 1, 0), new Vector2(0.5f, 0.75f)),
                // Left face
                new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector3(-1, 0, 0), new Vector2(0.25f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(-1, 0, 0), new Vector2(0.25f, 0.75f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(-1, 0, 0), new Vector2(0.0f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(-1, 0, 0), new Vector2(0.0f, 0.75f)),
                // Right face
                new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(1, 0, 0), new Vector2(0.5f, 0.75f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector3(1, 0, 0), new Vector2(0.5f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(1, 0, 0), new Vector2(0.75f, 0.75f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(1, 0, 0), new Vector2(0.75f, 0.5f)),
                // Back face
                new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector3(0, 0, -1), new Vector2(0.5f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector3(0, 0, -1), new Vector2(0.25f, 0.5f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(0, 0, -1), new Vector2(0.5f, 0.25f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0, 0, -1), new Vector2(0.25f, 0.25f)),
                // Bottom face
                new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0, -1, 0), new Vector2(0.5f, 0.25f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0, -1, 0), new Vector2(0.25f, 0.25f)),
                new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0, -1, 0), new Vector2(0.5f, 0.0f)),
                new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(0, -1, 0), new Vector2(0.25f, 0.0f))
        };

        public static int[] indices = new int[] { 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6, 8, 9, 10, 9, 11, 10, 12, 13, 14, 13, 15, 14, 16, 17, 18, 17, 19, 18, 20, 21, 22, 21, 23, 22 };

        public bool Transparent { get; set; }
        public ushort Id { get; set; }
        public Int16 X { get; set; }
        public Int16 Y { get; set; }
        public Int16 Z { get; set; }
    }
}
